<template>
  <div>
    <el-form :inline="true" :model="search">
      <el-form-item label="佣金类型">
        <el-select v-model="search.type" placeholder="请选择" size="small">
          <el-option
            v-for="item in search.typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="佣金状态">
        <el-select v-model="search.status" placeholder="请选择" size="small">
          <el-option
            v-for="item in search.statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="postSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="commissionList" border style="width: 100%">
      <el-table-column type="index" label="ID" width="50"></el-table-column>
      <el-table-column prop="type" label="佣金类型">
        <template slot-scope="scope">
          {{showType(scope.row.type)}}
        </template>
      </el-table-column>
      <el-table-column prop="commission" label="佣金(元)" width="200"></el-table-column>
      <el-table-column prop="status" label="状态" width="200">
        <template slot-scope="scope">
          {{showStatus(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="时间" width="200"></el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button type="primary" size="small" @click="apply">全部提现</el-button>
    </div>
    <div class="margin-top-20 text-center" v-if="commissionList.length">
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="currentPageChange"
        :background="true"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      search: {
        type: 'all',
        status: 0,
        typeOptions: [{
          value: 'all',
          label: '全部'
        },{
          value: 'consultation',
          label: '问诊'
        },{
          value: 'insurance',
          label: '保险'
        }],
        statusOptions: [{
          value: 0,
          label: '全部'
        },{
          value: 1,
          label: '未提现'
        },{
          value: 2,
          label: '待审核'
        },{
          value: 3,
          label: '已提现'
        }]
      },
      commissionList: [],
      page: {
        total: 0,
        currentPage: 1,
        size: 10
      }
    }
  },
  mounted() {
    this.getCommissionListData();
  },
  methods: {
    getCommissionListData() {
      let id = this.$store.state.doctorInfo.id;

      this.axios.get('/doctor/'+id+'/commission/record', {
        params: {
          pageSize: this.page.size,
          currentPage: this.page.currentPage,
          type: this.search.type,
          status: this.search.status
        }
      }).then((response) => {
        if(response.data.code == 200){
          this.commissionList = response.data.data.data;
          this.page.total = response.data.data.total;
        }else{
          this.$message({
            message: '数据加载失败!',
            type: 'error'
          });
        }
      });
    },
    postSearch() {
      this.getCommissionListData();
    },
    pageSizeChange(val) {
      this.page.currentPage = 1;
      this.page.size = val;
      this.getCommissionListData();
    },
    currentPageChange(val) {
      this.page.currentPage = val;
      this.getCommissionListData();
    },
    apply() {
      let id = this.$store.state.doctorInfo.id;

      this.axios.patch('/doctor/'+id+'/commission/apply').then((response) => {
        if(response.data.code == 200){
          this.commissionList.forEach(row => {
            if(row.status == 1){
              row.status = 2;
              row.isCheck = 0;
            }
          });
          
          this.$message({
            message: response.data.msg,
            type: 'success'
          });
        }else{
          this.$message({
            message: '提交申请失败，请重试!',
            type: 'error'
          });
        }
      })
    },
    showStatus(row) {
      let status = row.status;
      let isCheck = row.isCheck;

      switch(status){
        case 1:
          return isCheck == 1 ? '驳回申请' : '未提现';
          break;
        case 2:
          return '待审核';
          break;
        case 3:
          return '已结算';
          break;
        default:
          return '无';
          break;
      }
    },
    showType(type) {
      switch(type) {
        case 'consultation':
          return '问诊佣金';
          break;
        default:
          return '无';
          break;
      }
    }
  }
}
</script>
